export default class crossDomain {
  CheckInstallPluginObj = {
    isCheckInstallPlugin: false,
    resolve: null,
    reject: null,
    oldVersion: true,
  };
  CheckUrlOpen = {
    isCheckUrlOpen: false,
    resolve: null,
    reject: null,
    CheckUrlOpen: [],
  };
  sendMessageForPage = {
    resolve: null,
    reject: null,
  };
  controller = new AbortController();
  constructor(callBackFunc = function fn() {}) {
    this.callBackFunc = callBackFunc;
    this.initListen(this.callBackFunc);
  }
  getconter(e, that) {
    let obj = {
      check_install_plugin: (e) => {
        if (!e) {
          if (that.CheckInstallPluginObj.reject) {
            that.CheckInstallPluginObj.reject();
          }
          that.CheckInstallPluginObj.isCheckInstallPlugin = true;
          that.CheckInstallPluginObj.oldVersion = true;
        } else {
          let { data, code } = e;
          if (code == 200 && data == true) {
            that.CheckInstallPluginObj.isCheckInstallPlugin = true;
            that.CheckInstallPluginObj.oldVersion = false;
            that.CheckInstallPluginObj.resolve &&
              that.CheckInstallPluginObj.resolve(e);
          } else {
            that.CheckInstallPluginObj.reject &&
              that.CheckInstallPluginObj.reject(e);
          }
        }
      },
      check_url_open: (e) => {
        let { data, code } = e;
        if (code == 200 && data.length > 0) {
          that.CheckUrlOpen.isCheckInstallPlugin &&
            (that.CheckUrlOpen.isCheckInstallPlugin = true);
          that.CheckUrlOpen.resolve && that.CheckUrlOpen.resolve(e);
        } else {
          that.CheckUrlOpen.reject(e);
        }
      },
      send_message_for_page(e) {
        let { code } = e;
        if (code == 200) {
          that.sendMessageForPage.resolve && that.sendMessageForPage.resolve(e);
        } else {
          that.sendMessageForPage.reject && that.sendMessageForPage.reject(e);
        }
      },
    };
    if (e.data.type === "webpackClose") {
      this.closeListen();
      return;
    }
    if (
      e.data &&
      e.data.type_urs === "wkyy-wtbg" &&
      e.data.method === "send_dly_all"
    ) {
      let { data, api } = e.data;
      console.log(" data, api0..00031245", data, api);
      obj[api] && obj[api](data);
    } else if (
      e.data.method == "check_install_plugin" &&
      e.data.type_page == "pageMeat" &&
      e.data.type_urs == "wkyy-wtbg"
    ) {
      let { data, method } = e.data;
      obj[method] && obj[method](data);
    }
  }

  initListen() {
    window.addEventListener("message", this.callBackFunc);
    window.addEventListener(
      "message",
      (e) => {
        this.getconter(e, this);
      }
      //   { signal: this.controller.signal }
    );
  }
  closeListen() {
    window.removeEventListener("message", this.callBackFunc);
    this.controller.abort();
  }
  queryState() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(this);
      }, 1000);
    });
  }
  checkInstallPlugin() {
    return new Promise((resolve, reject) => {
      window.postMessage({
        type_urs: "wkyy-wtbg",
        type_page: "pageMeat",
        method: "check_install_plugin",
      });
      this.CheckInstallPluginObj.resolve = resolve;
      this.CheckInstallPluginObj.reject = reject;
    });
  }
  checkUrlOpen(
    data = [
      "https://compass.jinritemai.com/",
      "https://fxg.jinritemai.com/",
      "https://console.cloud.tencent.com/",
      "http://sss.com",
    ]
  ) {
    return new Promise((resolve, reject) => {
      window.postMessage({
        type_urs: "wkyy-wtbg",
        data,
        type_page: "pageMeat",
        method: "check_url_open",
      });
      this.CheckUrlOpen.resolve = resolve;
      this.CheckUrlOpen.reject = reject;
    });
  }
  sendMessageForPageFunc({ id, script }) {
    return new Promise((resolve, reject) => {
      window.postMessage({
        type_urs: "wkyy-wtbg",
        data: {
          tab_id: id,
          script,
        },
        type_page: "pageMeat",
        method: "inject_script",
      });
      this.sendMessageForPage.resolve = resolve;
      this.sendMessageForPage.reject = reject;
    });
  }
}
